class Solution
{
public:
    int minOperations(vector<int>& nums, int x)
    {
        int ret = -1, left = 0, right = 0, sum = 0, target = 0;
        for (auto e : nums) sum += e;

        target = sum - x;
        if (target < 0) return -1;

        for (sum = 0; right < nums.size(); right++)
        {
            sum += nums[right];

            while (sum > target) sum -= nums[left++];

            if (sum == target) ret = max(ret, right - left + 1);
        }

        return ret == -1 ? ret : nums.size() - ret;
    }
};